package com.cn.xiongmeng.czlxz.network;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.widget.TextView;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

import java.io.IOException;

public class HttpClient {

    private static final String TAG = "HttpClient";
    //private static final OkHttpClient client = new OkHttpClient();
    private static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");

    private OkHttpClient client;
    private Context context;
    private AlertDialog loadingDialog;
    public HttpClient(Context context) {
        this.client = new OkHttpClient();
        this.context = context;
    }
    // 请求回调接口
    public interface Callback {
        void onFailure(Exception e);
        void onResponse(String response);
    }
    // 异步，发送 POST 请求
    public void sendPostRequest(String url, String json, final Callback callback) {
        // showLoadingDialog();
        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        client.newCall(request).enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                // hideLoadingDialog();
                callback.onFailure(e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                // hideLoadingDialog();
                if (response.isSuccessful()) {
                    callback.onResponse(response.body().string());
                } else {
                    callback.onFailure(new IOException("Unexpected code " + response));
                }
            }
        });
    }
    // 显示正在加载提示
    /*private void showLoadingDialog() {
        if (loadingDialog == null) {
            // 使用AlertDialog进行自定义
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            // 使用ProgressBar展示加载中
            TextView textView = new TextView(context);
            textView.setText("正在加载...");
            textView.setTextColor(Color.BLACK);
            textView.setTextSize(20);
            textView.setGravity(android.view.Gravity.CENTER);

            // 设置对话框的背景色为whitesmoke
            builder.setView(textView);
            loadingDialog = builder.create();

            // 设置对话框宽度和背景色
            // loadingDialog.getWindow().setLayout(200, 200); // 设置对话框的宽度为600dp
            // loadingDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#F5F5F5"))); // 设置背景色为whitesmoke
            loadingDialog.setCancelable(false);

            *//*loadingDialog = new AlertDialog.Builder(context)
                    .setMessage("正在加载...")
                    .setCancelable(false)
                    .create();*//*
        }
        loadingDialog.show();
    }*/
    // 取消正在加载提示
    /*private void hideLoadingDialog() {
        if (loadingDialog != null && loadingDialog.isShowing()) {
            loadingDialog.dismiss();
        }
    }*/

    // 同步 POST 请求
    /*public static String postRequestSync(String url, String json) throws IOException {
        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                return response.body().string(); // 返回响应体
            } else {
                return "Request failed with code: " + response.code();
            }
        }
    }*/
    // 同步 GET 请求
    /*public static String getRequestSync(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();

        try (Response response = client.newCall(request).execute()) {
            if (response.isSuccessful()) {
                return response.body().string(); // 返回响应体
            } else {
                return "Request failed with code: " + response.code();
            }
        }
    }*/

}
